/**
 * @Descripttion: (QQ音乐爬虫)
 * @Author: Null-Pointer
 * @Date: 2021/7/28 10:19
 */
import axios from "axios";
import {Music,Singer,Album} from "./CommonObject.js";

//获取歌曲的vkey,需要歌曲的songMid
export async function getSongvkey({songMid}){
    const url = `/api/cgi-bin/musicu.fcg?format=json&data=%7B%22req_0%22:%7B%22module%22:%22vkey.GetVkeyServer%22,%22method%22:%22CgiGetVkey%22,%22param%22:%7B%22guid%22:%22358840384%22,%22songmid%22:%5B%22${songMid}%22%5D,%22songtype%22:%5B0%5D,%22uin%22:%221443481947%22,%22loginflag%22:1,%22platform%22:%2220%22%7D%7D,%22comm%22:%7B%22uin%22:%2218585073516%22,%22format%22:%22json%22,%22ct%22:24,%22cv%22:0%7D%7D`
    const result =(await axios.get(
        url
    )).data
    console.log( result.req_0.data.midurlinfo[0].purl)
    return result.req_0.data.midurlinfo[0].purl
}


//通过搜索可以获取一个歌曲列表，其中包含了每个歌曲的songMid
export async function getSearch ({keyword, page}) {
    let url = `http://c.y.qq.com/soso/fcgi-bin/client_search_cp?new_json=1&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=${page}&n=50&w=${encodeURIComponent(keyword)}&needNewCode=0`
    let {zhida, song: {list, totalnum, curpage}} = JSON.parse((await baseRequest(url)).data.slice(9, -1)).data // zhida ？ 直达 api 里面有中文
    console.log(JSON.parse((await axios(url)).data.slice(9, -1)).data)
    let direct
    switch (zhida.type) {
        case 1:
            direct = new Singer(zhida.zhida_singer.singerName, zhida.zhida_singer.singerMID, zhida.zhida_singer.singerPic)
            break;
        case 2:
            direct = new Album(zhida.zhida_album.albumName, zhida.zhida_album.albumMID)
            break;
    }
    return {direct, totalPage: Math.ceil(totalnum / 50),
        songList: list.map(
            ({name, mid, file: {media_mid}, singer, album, type, pay: {pay_play}}) =>
                new Music(
                    name, mid, media_mid,
                    new Album(album.name, album.mid),
                    singer.map(singerItem =>
                        new Singer(singerItem.name, singerItem.mid)),
                    type, pay_play))
    }
}

